<template>
  <div class="view_page empowerlist">
    <el-card class="box-card">
      <!--卡片头部-->
      <div slot="header" class="clearfix">
        <!--左侧-->
        <div class="left">
          <div class="menu-title">
            测点配置
          </div>
        </div>
        <div class="right">
          <el-button type="primary" size="small" @click="navtoAdd('add')"
            >新增
          </el-button>
        </div>
      </div>
      <!--筛选-->
      <div class="query-operate">
        <el-form
          class="demo-form-inline indoor-scene-buzhou-list-form"
          :inline="true"
          :model="TESTFROMDATA"
          @submit.native.prevent
        >
          <el-form-item
            label="气泡名称"
            label-width="120px"
            style="margin-top: 5px; margin-bottom: 5px;"
          >
            <el-input
              clearable
              @clear="searchList"
              v-model="TESTFROMDATA.name"
              placeholder="请输入气泡名称"
              @keyup.native.enter="searchList()"
            ></el-input>
          </el-form-item>
          <el-form-item
            style="margin-top: 5px; margin-bottom: 5px;margin-left: 10px;"
          >
            <el-button type="primary" @click="searchList()">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--卡片内容-->
      <!--列表-->
      <el-table :data="datalist" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center">
        </el-table-column>
        <el-table-column
          prop="bubble_name"
          label="气泡名称"
          width="140"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="water_level_site_name"
          label="水位计站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="rainfall_site_name"
          label="雨量计站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="meteorological_phenomena_site_name"
          label="气象监测站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="water_quality_site_name"
          label="水质监测站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="dam_site_name"
          label="大坝监测站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="yhz_site_name"
          label="溢洪闸监测站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="fsd_site_name"
          label="放水洞监测站点"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="230" align="center">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;flex-wrap: wrap; ">
              <el-button
                type="primary"
                size="small"
                @click="navtoAdd('update', scope.row.id)"
                >修改
              </el-button>
              <el-button type="danger" size="small" @click="del(scope.row)"
                >删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        :total="total"
        :currentPage="pageNum"
        :pageSize="size"
        position="right"
        @search="searchPagination"
      >
      </pagination>
    </el-card>

    <!--新增修改弹框-->
    <el-dialog
      :title="addtype == 'add' ? '新增测点配置' : '修改测点配置'"
      :visible.sync="dialogVisible"
      width="35%"
    >
      <el-form ref="form" label-width="140px">
        <el-form-item label="气泡" :required="true">
          <el-select
            filterable
            v-model="liveData.bubble_id"
            placeholder="请选择气泡"
          >
            <el-option
              v-for="item in qpData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="水位计站点" :required="true">
          <el-select
            filterable
            v-model="liveData.water_level_site"
            placeholder="请选择水位计站点"
          >
            <el-option
              v-for="item in swjData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="雨量计站点" :required="true">
          <el-select
            filterable
            v-model="liveData.rainfall_site"
            placeholder="请选择雨量计站点"
          >
            <el-option
              v-for="item in yljData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="气象监测站点" :required="true">
          <el-select
            filterable
            v-model="liveData.meteorological_phenomena_site"
            placeholder="请选择气象监测站点"
          >
            <el-option
              v-for="item in qxData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="水质监测站点" :required="true">
          <el-select
            filterable
            v-model="liveData.water_quality_site"
            placeholder="请选择水质监测站点"
          >
            <el-option
              v-for="item in szData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大坝监测站点" :required="true">
          <el-select
            filterable
            v-model="liveData.dam_site"
            placeholder="请选择大坝监测站点"
          >
            <el-option
              v-for="item in dbData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="溢洪闸监测站点" :required="true">
          <el-select
            filterable
            v-model="liveData.yhz_site"
            placeholder="请选择溢洪闸监测站点"
          >
            <el-option
              v-for="item in yhzData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="放水洞监测站点" :required="true">
          <el-select
            filterable
            v-model="liveData.fsd_site"
            placeholder="请选择放水洞监测站点"
          >
            <el-option
              v-for="item in fsdData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import pagination from "@/components/common/pagination/pagination";

export default {
  components: {
    pagination
  },
  data() {
    return {
      token: "",
      userInfo: JSON.parse(localStorage.getItem("loginPersonnelInformation")),
      // 模糊查询数据
      TESTFROMDATA: {
        name: ""
      },

      // 列表的数据
      datalist: [],
      pageNum: 1,
      page: 1,
      size: 10,
      total: 0,

      // 弹窗
      dialogVisible: false,
      // XQdialogVisible: false,
      addtype: "", //类型 新增-修改
      itemID: "", //记录选中的id

      qpData: [],
      swjData: [],
      yljData: [],
      qxData: [],
      szData: [],
      dbData: [],
      yhzData: [],
      fsdData: [],

      //富文本文档类型
      liveData: {
        bubble_id: "",
        water_level_site: "",
        rainfall_site: "",
        meteorological_phenomena_site: "",
        water_quality_site: "",
        dam_site: "",
        yhz_site: "",
        fsd_site: ""
      }
    };
  },
  created() {
    var _this = this;
    _this.u_get_token();
    _this.getListPage();
  },
  mounted() {},
  methods: {
    initData() {
      this.liveData = {
        bubble_id: "",
        water_level_site: "",
        rainfall_site: "",
        meteorological_phenomena_site: "",
        water_quality_site: "",
        dam_site: "",
        yhz_site: "",
        fsd_site: ""
      };
    },
    // 获取token
    u_get_token() {
      var _this = this;
      _this.$apis.u_get_token().then(res => {
        if (res.result == 0) {
          var token = res.data;
          sessionStorage.setItem("token", token);
          // 查询下拉
          _this.getSafeSiteList();
          _this.getBubbleList();
        }
      });
    },
    // 查询气泡
    getBubbleList() {
      var _this = this;
      // 气泡
      _this.$apis.u_monitor_bubble().then(res => {
        if (res.result == 0) {
          _this.qpData = res.data;
        }
      });
    },
    // 查询下拉
    getSafeSiteList() {
      var _this = this;
      // 水位计
      var datas = {
        type: 1
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.swjData = res.data;
        }
      });
      // 雨量计
      var datas = {
        type: 4
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.yljData = res.data;
        }
      });
      // 气象
      var datas = {
        type: 7
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.qxData = res.data;
        }
      });
      // 水质
      var datas = {
        type: 3
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.szData = res.data;
        }
      });
      // 大坝
      var datas = {
        type: 8
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.dbData = res.data;
        }
      });
      // 溢洪闸
      var datas = {
        type: 9
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.yhzData = res.data;
        }
      });
      // 放水洞
      var datas = {
        type: 10
      };
      _this.$apis.safe_site_list(datas).then(res => {
        if (res.result == 0) {
          _this.fsdData = res.data;
        }
      });
    },
    //查询测点配置列表
    getListPage() {
      var _this = this;
      var datas = {
        name: _this.TESTFROMDATA.name,
        page: _this.page,
        size: _this.size
      };
      _this.$apis.u_monitor_list(datas).then(res => {
        if (res.result == 0) {
          _this.datalist = res.data;
          _this.total = res.rows;
          _this.pageNum = res.page;
        }
      });
    },
    //模糊查询
    searchList() {
      this.page = 1;
      this.datalist = [];
      this.getListPage();
    },
    //分页
    searchPagination(page) {
      this.page = page;
      this.getListPage();
    },

    // 打开弹窗
    navtoAdd(type, item) {
      var _this = this;
      _this.getBubbleList();
      // console.log("打开弹窗type:", type, "item:", item);
      if (type == "add") {
        _this.addtype = "add";
        _this.initData();
        _this.dialogVisible = true;
      } else {
        _this.itemID = item;
        _this.addtype = "updata";
        var datas = {
          id: _this.itemID
        };
        // 查询详情
        _this.$apis.u_monitor_query(datas).then(res => {
          if (res.result == 0) {
            _this.liveData = res.data;
            _this.dialogVisible = true;
          }
        });
      }
    },
    //取消
    cancel() {
      this.dialogVisible = false;
    },
    save() {
      var _this = this;
      var datas = _this.liveData;
      // console.log("datas:", datas);
      if (datas.bubble_id == "") {
        Message.error("请选择气泡");
        return;
      }
      if (datas.water_level_site == "") {
        Message.error("请选择水位计站点");
        return;
      }
      if (datas.rainfall_site == "") {
        Message.error("请选择雨量计站点");
        return;
      }
      if (datas.meteorological_phenomena_site == "") {
        Message.error("请选择气象监测站点");
        return;
      }
      if (datas.water_quality_site == "") {
        Message.error("请选择水质监测站点");
        return;
      }
      if (datas.dam_site == "") {
        Message.error("请选择大坝监测站点");
        return;
      }
      if (datas.yhz_site == "") {
        Message.error("请选择溢洪闸监测站点");
        return;
      }
      if (datas.fsd_site == "") {
        Message.error("请选择放水洞监测站点");
        return;
      }
      if (_this.addtype == "add") {
        _this.$apis.u_monitor_add(datas).then(res => {
          if (res.result == 0) {
            Message.success("添加成功");
            this.dialogVisible = false;
            this.getListPage();
          } else {
            this.dialogVisible = false;
            this.getListPage();
          }
        });
      } else {
        datas.id = _this.itemID;
        _this.$apis.u_monitor_update(datas).then(res => {
          if (res.result == 0) {
            Message.success("修改成功");
            this.dialogVisible = false;
            this.getListPage();
          } else {
            this.dialogVisible = false;
            this.getListPage();
          }
        });
      }
    },
    // 删除
    del(item) {
      var _this = this;
      var datas = {
        id: item.id
      };
      _this
        .$confirm("确定要删除【" + item.bubble_name + "】吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          _this.$apis.u_monitor_del(datas).then(res => {
            if (res.result == 0) {
              Message.success("删除成功");
              if (_this.page > 1 && _this.datalist.length <= 1) {
                _this.page = _this.page - 1;
              }
            }
            //查询列表
            _this.getListPage();
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style>
.empowerlist .el-dialog__header {
  border-bottom: 1px solid #e3e3e3;
}
.column-bz {
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
}
.empower-time {
  width: 100% !important;
}
/**站内信长单词强制换行 */
.message_box_alert {
  word-break: break-all !important;
}
.key-content .el-form-item__content {
  margin-left: 0 !important;
}
</style>
